// 控制更多查询条件的显示和隐藏
$(".layui-searchM").hide();
$(".layui-search-more").click(function (event) {
    if ($(".layui-searchM").css('display') == "none") {
        $(".layui-search-more .layui-icon").html("&#xe619;");
    } else {
        $(".layui-search-more .layui-icon").html("&#xe61a;");
    }
    $(".layui-searchM").slideToggle();
});



// zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
var setting = {};
// zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
var zNodes = [
	{
        name: "节点1",
        open: true,
        children: [
			{
            	name: "节点1_1"
			},
			{
				name: "节点1_2"
			},
			{
				name: "节点1_3"
			},
			{
				name: "节点1_4",
        		open: true,
				children: [
					{
						name: "节点1_4_1"
					},
					{
						name: "节点1_4_2"
					},
					{
						name: "节点1_4_3"
					},
					{
						name: "节点1_4_4",
						open: true,
						children: [
							{
								name: "节点1_4_4_1"
							},
							{
								name: "节点1_4_4_2"
							},
							{
								name: "节点1_4_4_3"
							},
							{
								name: "节点1_4_4_4",
								open: true,
								children: [
									{
										name: "节点1_4_4_4_1"
									},
									{
										name: "节点1_4_4_4_2"
									},
									{
										name: "节点1_4_4_4_3"
									},
								]
							},
						]
					},
				]
			},
		]
    },
	{
        name: "节点2",
        open: true,
        children: [
			{
            	name: "节点2_1"
			},
			{
				name: "节点2_2"
			},
		]
    },
	{
        name: "节点3",
        open: true,
        children: [
			{
            	name: "节点3_1"
			},
			{
				name: "节点3_2"
			},
		]
    },
];
$(document).ready(function () {
    zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
});


layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //示例列表
    var tableIns = table.render({
        elem: '#egList',

        //测试效果，暂时用的假数据
        url: '/json/newsList.json',
        page: true,
        cellMinWidth : 95,
        height : "full-160",
        limit : 15,
        limits : [10,15,20,25],
        id : "egListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            
            {field: 'newsName', title: '文章标题', width: 400 },
            {field: 'newsAuthor', title: '作者', align: 'center' },
            {field: 'newsLook', title: '权限', align:'center'},
            {field: 'newsStatus', title: '状态', align:'center',templet:function (data) {
                if(data.newsStatus == 0){
                    return "<span>本地草稿</span>"
                }else if(data.newsStatus == 1){
                    return "<span style='color: #1E9FFF;'>待审核</span>"
                }else if(data.newsStatus == 2 ){
                    return "<span style='color: #FF5722;'>未通过</span>"
                }else if(data.newsStatus == 3 ){
                    return "<span style='color: #009688;'>已通过</span>"
                }
            }},
            
            {field: 'newsTime', title: '发布时间', align:'center', minWidth:200},
            {title: '操作', width: 150, fixed: "right", templet:'#egListBar',fixed:"right",align:"center"}
        ]]
    });

    //是否置顶
    form.on('switch(egTop)', function (data) {
        var index = layer.msg('修改中，请稍候', { icon: 16, time: false, shade: 0.8 });
        setTimeout(function () {
            layer.close(index);
            if (data.elem.checked) {
                layer.msg("置顶成功！");
            } else {
                layer.msg("取消置顶成功！");
            }
        }, 500);
    })

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click", function () {
        if ($(".searchVal").val() != '') {
            table.reload("egListTable", {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    egTitle: $(".searchVal").val()
                    /*  key: $(".searchVal").val() */ //搜索的关键字
                }
            })
        } else {
            table.reload("egListTable", {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    egTitle: ""
                    /*  key: $(".searchVal").val() */ //搜索的关键字
                }
            })
            //  layer.msg("请输入搜索的内容");
        }
    });

    //添加文章
    function addeg(edit) {
        var index = layui.layer.open({
            title: "新增",
            type: 2,
            content: "form01.html",
            success: function (layero, index) {
                setTimeout(function () {
                    layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize", function () {
            layui.layer.full(index);
        })
    }
    $(".addeg_btn").click(function () {
        addeg();
    })

    //批量删除
    $(".delAll_btn").click(function () {
        var checkStatus = table.checkStatus('egListTable'),
            data = checkStatus.data,
            id = "";
        if (data.length > 0) {
            for (var i in data) {
                id += (data[i].id + ",");
            }
            layer.confirm('确定删除选中的文章？', { icon: 3, title: '提示信息' }, function (index) {
                $.get(layuiFunciton.URL + "/eg/deleteEntity", {
                    id: id  //将需要删除的egId作为参数传入
                }, function (data) {
                    tableIns.reload();
                    layer.close(index);
                })
            })
        } else {
            layer.msg("请选择需要删除的文章");
        }
    })



    //列表操作
    table.on('tool(egList)', function (obj) {
        var checkStatus = obj.data;
        var layEvent = obj.event,//获得 lay-event 对应的值
            data = obj.data;//获得当前行数据
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        console.log(checkStatus)

        var updateUserLayer = "";
        if (layEvent === 'edit') { //编辑  修改
            //点击编辑按钮
            var index = layui.layer.open({
                title: "修改",
                type: 2,
                offset: '10px',//水平居中，垂直100px
                area: ['700px', '550px'],//自定义宽高
                content: "form01.html",
                success: function (layero, index) {
                    var body = layui.layer.getChildFrame('body', index);
                    if (data) {
                        body.find("#id").val(checkStatus.id);
                        body.find("#egTitle").val(checkStatus.egTitle);
                        body.find("#egContent").val(checkStatus.egContent);
                        body.find("#author").val(checkStatus.author);
                        body.find("#topStatus").val(checkStatus.topStatus);
                        body.find("#egource").val(checkStatus.egource);
                        body.find("#publisher").val(checkStatus.publisher);
                        body.find("#publishTime").val(checkStatus.publishTime);
                        form.render();
                    }
                    setTimeout(function () {
                        layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        }, 500);
                    })
                }
            })
            layui.layer.full(index);

            // addeg(data);
        } else if (layEvent === 'del') { //删除
            layer.confirm('确定删除此文章？', { icon: 3, title: '提示信息' }, function (index) {
                $.get(layuiFunciton.URL + "/eg/deleteEntity", {
                    id: data.id //将需要删除的egId作为参数传入
                }, function (data) {
                    tableIns.reload();
                    layer.close(index);
                })
            });
        } else if (layEvent === 'look') { //预览 详情
            //  viewLog(data);

            //  layer.alert("此功能需要前台展示，实际开发中传入对应的必要参数进行文章内容页面访问")
            var index = layui.layer.open({
                title: "详情",
                type: 2,
                content: "form-detail01.html",
                success: function (layero, index) {
                    var body = layui.layer.getChildFrame('body', index);
                    if (data) {
                        body.find("#id").val(checkStatus.id);
                        body.find("#egTitle").val(checkStatus.egTitle);
                        body.find("#egContent").val(checkStatus.egContent);
                        body.find("#author").val(checkStatus.author);
                        body.find("#topStatus").val(checkStatus.topStatus);
                        body.find("#egource").val(checkStatus.egource);
                        body.find("#publisher").val(checkStatus.publisher);
                        body.find("#publishTime").val(checkStatus.publishTime);
                        form.render();
                    }
                    setTimeout(function (data) {
                        layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }, 500)
                }
            })
            layui.layer.full(index);
        }
    });


    //修改提交表单
    form.on("submit(updateSubmit)", function (data) {
        //弹出loading
        // var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        // 实际使用时的提交信息
        $.post(layuiFunciton.URL + "/eg/editEntity", {
            id: $("#id").val(),
            egTitle: $("#egTitle").val(),  //文章标题
            egContent: $("#egContent").val(), //文章内容
            topStatus: $("#topStatus").val(),  //置顶状态
            author: $("#author").val(),     //作者
            egource: $("#egource").val(),///新闻来源
            publisher: $("#publisher").val(),///发布人姓名
            publishTime: $("#publishTime").val(),///发布时间
        }, function (res) {

        })
        setTimeout(function () {

            top.layer.msg("修改成功！");
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();
        }, 500);
        return false;
    })

})

